package com.ittx.dzt.meituan.service;

import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.SkuQueryDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.meituan.constant.MeituanApiConstant;
import com.ittx.dzt.meituan.handler.AbstractBaseMeituanApiHandler;
import com.ittx.dzt.sys.response.Result;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据spuid 获取商品详情
 * <p>
 * cy.sku.download.bySpuID
 * /api/v1/gw/goods/detail (查询商品详情)  https://tscc.meituan.com/home/doc/bulkPurchasing/546
 * </p>
 *
 * @author wushixiong
 * @since 2022/5/6 17:18
 */
@Slf4j
@Service
public class MeituanSkuDownloadBySpuIdService extends AbstractBaseMeituanApiHandler<SkuQueryDTO> {


    @Override
    public String getLocalApiMethod() {
        return PlatformApiConstant.InboundInterface.SKU_DOWNLOAD_BY_PLATFORMSKUID;
    }

    @Override
    public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, SkuQueryDTO skuQueryDTO) {

        List<Object> list = new ArrayList<>();
        for (String spuId : skuQueryDTO.getPlatformSpuId().split(",")) {
            // 请求接口
            Result<?> result = this.getProductById(spuId, platformApiParamDTO);
            if (!result.isSuccess()) {
                return result;
            }
            Object resultData = result.getData();
            // 同步返回
            if (skuQueryDTO.getIsAsync() == 0) {
                list.add(resultData);
            } else {
                platformApiParamDTO.setPlatformIdentifierTag(spuId);
                result = this.sendRequestToPlatform(platformApiParamDTO, resultData, true);
                if (result.hasError()) {
                    return result;
                }
            }
        }
        // 同步返回
        if (skuQueryDTO.getIsAsync() == 0) {
            return Result.success().addData(list).addMsg(platformApiParamDTO.getStoreConfig().getChainUniqueCode());
        }
        return Result.success();
    }

    /**
     * 根据id获取商品
     *
     * @param id
     * @param platformApiParamDTO
     * @return
     */
    public Result<?> getProductById(String id, PlatformApiParamDTO platformApiParamDTO) {

        return this.getProductBySdk(id, platformApiParamDTO);
    }


    /**
     * sdk获取商品
     *
     * @param productId 商品id
     */
    public Result<?> getProductBySdk(String productId, PlatformApiParamDTO platformApiParamDTO) {
        Map<String, String> params = new HashMap<>();
        StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
        String requestId = platformApiParamDTO.getRequestId();
        // 商品Id

        params.put("goods_id", productId);
        params.put("poi_id", getPoIId(platformApiParamDTO));
        Result<?> result = this.executeMeituanApi(storeConfig, params, requestId);
        if (!result.isSuccess()) {
            return result;
        }
        Map<String, Object> resultMap = (Map<String, Object>) result.getData();
        return Result.success().addData(resultMap.get(DATA_FIELD));

    }

    @Override
    public String getMeituanMethod() {
        return MeituanApiConstant.GOODS_DETAIL;
    }

    @Override
    public String getHttpMethod() {
        return MeituanApiConstant.ApiHttpMethod.POST;
    }
}
